/*
 *
 * Copyright (c) 2004-2010.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY ARIZONA STATE UNIVERSITY ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARIZONA STATE UNIVERSITY
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package supermemo.input;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

import supermemo.dictcn.WordFileConverter;
import supermemo.input.ref.FileModel;
import flanagan.io.FileChooser;

/**
 * @author <a href='mailto:Yan.Qi@asu.edu'>Yan Qi</a>
 * @version $Revision$
 * @latest $Id$
 */
public class MainFrame extends JFrame
{
	/**
	 * Default constructor. 
	 */
	public MainFrame()
	{
		super();		

		//// Basic parameters for initiation
		setTitle("SuperMemo Convertor");
		setName("MainWin");

		setLayout(new BorderLayout());
		setBounds(100, 100, 800, 600);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Adjust the initial position of the frame
		UtilCenter.Move2ScreenCenter(this);

		////////////////////////////////////////////////////
		JTable jt = init_menu_toolbar();
		final JScrollPane file_panel = new JScrollPane(jt);
		
		////////////////////////////////////////////////////
		//// Split-Panel starts
		getContentPane().add(file_panel, BorderLayout.CENTER);

	}
	
	private JTable init_menu_toolbar()
	{
		JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        final JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);
		
		final JComboBox time_combox = new JComboBox();
//		final List<String> file_list = new ArrayList<String>();
		final List<Pair<String, String>> file_pair_list = new ArrayList<Pair<String,String>>();
		final Set<String> time_list = new TreeSet<String>();
		final List<WordFileConverter> convertor_list = new ArrayList<WordFileConverter>();
		final FileModel fm = new FileModel();
	    JTable jt = new JTable(fm);
//	    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	    jt.setColumnSelectionAllowed(true);
		
	    // 1. add an item for "File Open"
		final JMenuItem openMenuItem = new JMenuItem("Open", 'O');
		ImageIcon icon_open = new ImageIcon("res/icon/open.gif");
		Action open_menu_action = new AbstractAction("Open", icon_open) 
		{

			public void actionPerformed(ActionEvent arg0)
			{
				FileChooser fc = new  FileChooser("data/");
				fc.setExtension("xml");
				String sel_file_path = fc.selectFile();
		        String strFile=fc.getDirPath()+'/'+sel_file_path;
		        if(sel_file_path!=null)
		        { 
		        	file_pair_list.add(new Pair<String,String>(strFile, 
		        			strFile.replace(".xml", "_superMemo.xml")));
		        	fm.setFileStats(file_pair_list);
		        	WordFileConverter cur_convertor = new WordFileConverter(strFile); 
		        	convertor_list.add(cur_convertor);
		        	time_list.addAll(cur_convertor.getTimeSet());
//		        	System.out.println(strFile);
		        }
		        
		        // update the list of the combox
		        time_combox.removeAllItems();
		        time_combox.addItem("");
		        for(String cur_time : time_list)
		        {
		        	time_combox.addItem(cur_time);
		        }
		        time_combox.setEnabled(true);
			}
			
		}; 
		openMenuItem.addActionListener(open_menu_action);
		fileMenu.add(openMenuItem);
		
		// 2. Add an item for "File Conversion"
		final JMenuItem convMenuItem = new JMenuItem("Convert", 'C');
		ImageIcon icon_conv = new ImageIcon("res/icon/StateflowEvent.gif");
		Action convert_menu_action = new AbstractAction("Convert", icon_conv) {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				for(int i=0; i<convertor_list.size(); ++i)
				{
					WordFileConverter cur_convertor = convertor_list.get(i);
					String cur_target_file = file_pair_list.get(i).second();
					String sel_time = time_combox.getSelectedItem().toString();
					System.out.println("Selected Combox is : "+sel_time);
					cur_convertor.export_to_file(cur_target_file, sel_time);
				}
			}
		};
		convMenuItem.addActionListener(convert_menu_action);
		fileMenu.add(convMenuItem);
		
		// MenuItem "Exit"
		final JMenuItem exitMenuItem = new JMenuItem("Exit", 'E');
		exitMenuItem.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
			
		});
		fileMenu.add(exitMenuItem);
		
		/////////////////////////////////////////////////////////////
		// start working on ToolBar
		JToolBar toolBar = new JToolBar();
		this.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		JButton open_btn = toolBar.add(open_menu_action);
		open_btn.setToolTipText("Open Source XML file");

		JButton convert_btn = toolBar.add(convert_menu_action);
		convert_btn.setToolTipText("Covert it into SuperMemo file");
		
		toolBar.addSeparator();
		toolBar.addSeparator();
		
		JLabel time_combox_tip = new JLabel("Convert words no earlier than:"); 
		toolBar.add(time_combox_tip);
		toolBar.addSeparator();
		
//		String course[] = {"BCA","MCA","PPC","CIC"};
		time_combox.setEnabled(false);
		toolBar.add(time_combox);
		
		return jt;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		System.out.println("Welcome to the class MainFrame!");
		MainFrame frame = new MainFrame();
		frame.setVisible(true);
	}

}
